package com.kaifamiao.stock.controller;

import com.kaifamiao.stock.domain.Stock;
import com.kaifamiao.stock.service.StockService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping
public class StockController {

    @Resource
    private StockService stockService;

    @GetMapping("/all")
    public List<Stock> selectAll() {
        return stockService.selectAll();
    }

//  查找某一只股票
    @GetMapping("/selectOne/{code}")
    public List<Stock> selectByName(@PathVariable("code") String code) {
        return stockService.selectByName(code);
    }

//    查找所有股票
    @GetMapping("/selectAll")
    public List<Stock> selectByAllName() {
        return stockService.selectByAllName();
    }
//      查找某一只股票在某个时间两个月前到最新日期的所有股票信息
    @GetMapping("/selectTime")
    public List<Stock> selectByTime(@RequestParam("code") String code, @RequestParam("date") String date) {
        System.out.println("date = " + date);
        Date date1 = null;
        try {
            date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return stockService.selectByEndTime(code, date1);
    }

}
